/*******************************************************************************
 * Copyright (c) 2023 Gradle Inc. and others
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 ******************************************************************************/
package org.eclipse.buildship.ui.internal.wizard.project;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.CoreException;

import org.eclipse.buildship.core.internal.operation.ToolingApiStatus;

/**
 * Helper method for the import wizards.
 *
 * @author Donat Csikos
 */
public class WizardHelper {

    private WizardHelper() {
    }

    /**
     * Converts the target {@link InvocationTargetException} generated by {@code IWizardContainer} into a {@code ToolingApiStatus}.
     *
     * @param exception the target exception
     * @return the new status object
     */
    static ToolingApiStatus containerExceptionToToolingApiStatus(InvocationTargetException exception) {
        Throwable target = exception.getTargetException() == null ? exception : exception.getTargetException();
        if (target instanceof CoreException && ((CoreException) target).getStatus() instanceof ToolingApiStatus) {
           return (ToolingApiStatus) ((CoreException) target).getStatus();
        } else {
            return ToolingApiStatus.from("Project import", target);
        }
    }
}
